﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TryHelp;

namespace Helper
{
    public partial class Form1 : Form
    {
        private UserInfo user;
        private int tick = 10;
        public Form1()
        {
            InitializeComponent();
            user = new UserInfo(MachineCode.Value(), 10);
            InitClick();
            label1.Text = string.Format("当前用户：{0}", user.MachineCode);
        }
        void InitClick(int n = 0)
        {
            if (user.ClickCount >= 60)
            {
                Alert.Info("当前点击数已到最大值，请提交链接后等待其他小伙伴点击！");
                return;
            }
            user.ClickCount += n;
            label2.Text = string.Format("当前可用点击数：{0}", user.ClickCount);
        }
        void ticking()
        {
            if (tick == 0)
            {
                linkLabel1.Text = "刷新数据";
                linkLabel1.Enabled = true;
                tick = 10;
                timer1.Stop();
            }
            else
            {
                linkLabel1.Text = string.Format("刷新数据({0})", tick);
                tick--;
                linkLabel1.Enabled = false;
            }
        }
        IList<MainViewModel> genData()
        {
            var list = new List<MainViewModel>();
            for (int i = 0; i < 100; i++)
            {
                list.Add(new MainViewModel()
                {
                    id = i.ToString(),
                    add = string.Format("http://tv.wandhi.com/helper/list", i),
                    click_count = new Random(DateTime.Now.Millisecond).Next()
                });
            }
            return list;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //dataGridView1.DataSource = genData();
            dataGridView1.DataSource = getData();

        }
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <returns></returns>
        private List<MainViewModel> getData()
        {
            List<MainViewModel> l = new List<MainViewModel>();

            var rq = new DNetRequest();
            rq.SetData("user", user.MachineCode);
            rq.SetData("token", user.code);
            rq.SetData("time", DateTime.Now.Ticks);
            var res = RO.Post(rq, "http://tv.wandhi.com/api/helper");
            if (res.Success)
            {
                l = JsonConvert.DeserializeObject<List<MainViewModel>>((string)res.Data);
            }
            return l;
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor != Color.Red)
            {
                var row = dataGridView1.Rows[e.RowIndex];
                var url = row.Cells[1].Value.ToString();
                Process.Start(url);
                InitClick(1);
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                var q = new DNetRequest();
                q.SetData("id", row.Cells[0].Value.ToString());
                q.SetData("user", user.MachineCode);
                q.SetData("token", user.code);
                q.SetData("time", DateTime.Now.Ticks);
                RO.PostAsync(q, "http://tv.wandhi.com/api/helper/up");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            if (user._ClickCount / 100 < 1)
            {
                Alert.Error("无可用点击，请先点击别人的链接！（我为人人，人人为我！）");
                return;
            }
            if (!Alert.Confirm("确定提交链接吗？(注：我们不会对链接的可用性进行检测！)"))
            {
                return;
            }
            var link = textBox1.Text;
            if (string.IsNullOrEmpty(link))
            {
                Alert.Info("请输入推广链接！");
                return;
            }
            var q = new DNetRequest();
            q.SetData("link", link);
            q.SetData("count", user._ClickCount / 100);
            q.SetData("user", user.MachineCode);
            q.SetData("token", user.code);
            q.SetData("time", DateTime.Now.Ticks);

            var res = RO.Post(q, "http://tv.wandhi.com/api/helper/add");
            if (res.Success)
            {
                var _r = Convert.ToInt32(res.Data);
                if (_r == 3)
                {
                    Alert.Info("请输入合法的网站地址！(以http或https开头)");
                }
                else if (_r == 2)
                {
                    Alert.Info("您之前提交的链接还没被点击完成，请稍后再添加！");
                }
                else
                {
                    Alert.Info("添加成功");
                }
            }
            else
            {
                Alert.Info("数据异常，请稍后再试");
            }
            button1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ticking();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            dataGridView1.DataSource = getData();
            timer1.Start();
        }
    }
}
